﻿using Dapper;
using Domain.Entity.ord;
using Domain.IRepository;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repository
{
    public class Purchase_dRepository : IPurchase_dRepository
    {

        //连接字符串
        string con = "Data Source=121.37.93.71;Initial Catalog=CSSL_ZCTWMS;User ID=sa;Password=geili@2025;Encrypt=False";

        //新增进货明细单
        public async Task<int> AddPurchase_d(Purchase_d purchase_D)
        {
            using (SqlConnection sqlConnection = new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();
                string sql = $@"insert into [dbo].[ord_purchase_d](pur_id, proc_id, proc_name, proc_pur_num, proc_pur_price, proc_pur_total, remark)
                                values({purchase_D.pur_id},{purchase_D.proc_id},'{purchase_D.proc_name}',{purchase_D.proc_pur_num},'{purchase_D.proc_pur_price}','{purchase_D.proc_pur_total}','{purchase_D.remark}')";
                return await sqlConnection.ExecuteAsync(sql);
            }
        }


        //根据进货单id查询明细
        public async Task<List<Purchase_d>> GetPurchase_Ds(int purid)
        {
            using (SqlConnection sqlConnection = new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();
                string sql = $@"select * from [dbo].[ord_purchase_d] where pur_id={purid}";
                return sqlConnection.Query<Purchase_d>(sql).ToList();
            }
        }


        //修改进货单明细
        public async Task<int> DeletePurchase_d(int purid)
        {
            using (SqlConnection sqlConnection = new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();
                string sql = $@"delete from [dbo].[ord_purchase_d] where pur_id={purid}";
                return await sqlConnection.ExecuteAsync(sql);
            }
        }


        //更新入库数量
        public async Task<int> Updatenum(Purchase_d purchase_D)
        {
            using (SqlConnection sqlConnection = new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();
                string sql = $@"update [dbo].[ord_purchase_d] set purd_instockOK_num={purchase_D.purd_instockOK_num},purd_instockNo_num={purchase_D.purd_instockNo_num} where purd_id={purchase_D.purd_id}";
                return await sqlConnection.ExecuteAsync(sql);
            }
        }

        //根据进货单与货品获取到主键id
        public async Task<int> Getid(int purid, int procid)
        {
            using (SqlConnection sqlConnection = new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();
                string sql = $"select purd_id from [dbo].[ord_purchase_d] where pur_id={purid} and proc_id={procid}";
                return await sqlConnection.ExecuteScalarAsync<int>(sql);
            }
        }


        //更新已退货数量
        public async Task<int> UpdateRNum(int purtid, int returnnum)
        {
            using (SqlConnection sqlConnection = new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();
                string sql = $@"update [dbo].[ord_purchase_d] set purd_returned_num={returnnum} where purd_id={purtid}";
                return await sqlConnection.ExecuteAsync(sql);
            }
        }

        public async Task<int> UpdateRNum(int purtid, int returnnum, int newnum)
        {
            using (SqlConnection sqlConnection = new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();
                string sql = $@"update [dbo].[ord_purchase_d] set purd_returned_num={returnnum},proc_pur_num={newnum} where purd_id={purtid}";
                return await sqlConnection.ExecuteAsync(sql);
            }
        }
    }
}
